home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1991 / 06 / alib / startup2.asm < prev    next >
Assembly Source File  |  1991-08-22  |  3KB  |  147 lines

  1.     title    start up code for assembly langauge programs
  2.     include    asm.inc
  3.  
  4.     public    startup
  5.  
  6.     dosseg                ; use standard segment ordering
  7. NULL    segment    para public 'BEGDATA'    ; paragraph align DGROUP
  8. NULL    ends
  9.  
  10. XIB    segment word public 'DATA'    ; these segments contain a list of
  11. xibegin    label    word            ;  procedure addresses that the
  12. XIB    ends                ;  start-up code calls before main
  13. XI    segment word public 'DATA'
  14. XI    ends
  15. XIE    segment word public 'DATA'
  16. xiend    label    word
  17. XIE    ends
  18.  
  19. XPB    segment word public 'DATA'    ; these segments contain a list of
  20. xpbegin    label    word            ;  procedure addresses that the
  21. XPB    ends                ;  start-up code calls before
  22. XP    segment word public 'DATA'    ;  returning control to ms-dos.  they
  23. XP    ends                ;  are called only on a full exit.
  24. XPE    segment word public 'DATA'    ;  
  25. xpend    label    word
  26. XPE    ends
  27.  
  28. XCB    segment word public 'DATA'    ; these segments contain a list of
  29. xcbegin    label    word            ;  procedure addresses that the
  30. XCB    ends                ;  start-up code calls before
  31. XC    segment word public 'DATA'    ;  returning control to ms-dos.  they
  32. XC    ends                ;  are ALWAYS called regardless of how
  33. XCE    segment word public 'DATA'    ;  the program terminates.
  34. xcend    label    word
  35. XCE    ends
  36.  
  37. DGROUP    group    XIB,XI,XIE,XPB,XP,XPE,XCB,XC,XCE
  38.  
  39.     .stack
  40.     .data
  41.     public    psp,dgroup_segment,top_of_stack,initterm,xcbegin,xcend
  42.  
  43. psp            dw    0
  44. dgroup_segment        dw    0
  45.  
  46.  
  47.     .data?
  48.     extb    _edata,_end
  49.  
  50. top_of_stack        dw    ?
  51.  
  52.  
  53.     .code
  54.     extn    set_argc_argv,ms_dos,main,exit
  55.  
  56.  
  57. ;;    initterm
  58. ;
  59. ;    entry    SI    initializer or terminator list
  60. ;        DI    end of list
  61. ;    may use    AX,BX,CX,DX,DI,SI,DS,ES
  62. ;
  63. initterm proc
  64.     movx    ds,DGROUP_SEGMENT    ; call list of initializers or
  65. iit1:    cmp    si,di            ;  terminators
  66.     jae    iit2            ;   if end of list
  67.     lodsw
  68.     or    ax,ax
  69.     jz    iit1            ;   if NULL address
  70.  
  71.     pushm    di,si,ds
  72.     call    ax
  73.     popm    ds,si,di
  74.     jmp    iit1
  75. iit2:    ret
  76. initterm endp
  77.  
  78.  
  79. ;;    startup
  80. ;
  81. ;    entry    AX    offset stack
  82. ;        ES    program segment prefix
  83. ;        SS    DGROUP
  84. ;    exit    BP    0
  85. ;    may use    AX,BX,CX,DX,DI,SI,DS,ES
  86. ;
  87. startup proc
  88.     cld
  89.     movx    bp,0            ; BP=0 in TIGHT model
  90.  
  91.     mov    ax,ss            ; compensate for Microsoft LINK bug
  92.     mov    dx,DGROUP        ;  where SS is wrong segment
  93.     sub    ax,dx
  94.     jbe    sta1            ;  if SS is DGROUP, must be OPTLINK
  95.     mov    cl,4            ;  else MS-LINK, fix stack pointer
  96.     shl    ax,cl
  97.     mov    ss,dx
  98.     add    sp,ax
  99.  
  100. sta1:    mov    top_of_stack[bp],sp
  101.     mov    psp[bp],es
  102.  
  103.     mov    ax,sp            ; compute number of paragraphs
  104.     add    ax,15            ;  in program
  105.     mov    cl,4
  106.     shr    ax,cl
  107.     mov    bx,ss
  108.     add    bx,ax
  109.     sub    bx,psp[bp]
  110.  
  111.     mov    ah,4Ah            ; shrink program memory block for
  112.     call    ms_dos            ;  dynamic storage system
  113.  
  114.     mov    dgroup_segment[bp],ss
  115.  
  116.     mov    al,0            ; zero BSS segment
  117.     mov    cx,offset DGROUP:_end
  118.     movx    es,DGROUP_SEGMENT
  119.     mov    di,offset DGROUP:_edata
  120.     sub    cx,di
  121.     rep    stosb
  122.  
  123.     call    set_argc_argv        ; process command line
  124.  
  125.     lea    di,xiend        ; call initializers
  126.     lea    si,xibegin
  127.     call    initterm
  128.  
  129.     movx    ds,DGROUP_SEGMENT    ; call user's program
  130.     movx    es,DGROUP_SEGMENT
  131.     call    main
  132.  
  133.     push    ax
  134.     lea    di,xpend        ; call on-exit routines
  135.     lea    si,xpbegin        ;  (must call on-exits before
  136.     call    initterm        ;   terminators!)
  137.  
  138.     lea    di,xcend        ; call terminators
  139.     lea    si,xcbegin
  140.     call    initterm
  141.     pop    ax
  142.  
  143.     jmp    exit
  144. startup endp
  145.  
  146.     end    startup
  147.